<template>
  <div class="layout">
    <el-container class="container" v-if="showMenu">
      <el-aside class="aside">
        <!--系统名称+logo-->
        <div class="head">
          <div>
            <img src="//s.weituibao.com/1582958061265/mlogo.png" alt="logo">
            <span>admin-zyx</span>
          </div>
        </div>
        <!--一条为了美观的线条-->
        <div class="line" />
        <!-- el-menu 有一个router属性，是否使用vue-router的模式，启用该模式会在激活时以index作为path进行路由跳转 -->
        <el-menu
          background-color="#222832"
          text-color="#fff"
          :router="true"
          :default-openeds="defaultOpen"
          :default-active='currentPath'
        >
          <!--一级栏目-->
          <el-submenu index="1">
            <template #title>
              <span>Dashboard</span>
            </template>
            <!--二级栏目-->
            <el-menu-item-group>
              <el-menu-item index="/"><i class="el-icon-data-line" />首页</el-menu-item>
              <el-menu-item index="/add"><i class="el-icon-data-line" />添加商品</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu>
            <template #title>
              <span>首页配置</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/swiper"><i class="el-icon-picture"></i>轮播图配置</el-menu-item>
              <el-menu-item index="/hot"><i class="el-icon-picture"></i>热销商品配置</el-menu-item>
              <el-menu-item index="/new"><i class="el-icon-picture"></i>新品上线配置</el-menu-item>
              <el-menu-item index="/recommend"><i class="el-icon-picture"></i>为你推荐配置</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu>
            <template #title>
              <span>模块管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/category"><i class="el-icon-picture"></i>分类管理</el-menu-item>
              <el-menu-item index="/good"><i class="el-icon-picture"></i>商品管理</el-menu-item>
              <el-menu-item index="/guest"><i class="el-icon-picture"></i>会员管理</el-menu-item>
              <el-menu-item index="/order"><i class="el-icon-picture"></i>订单管理</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu>
            <template #title>
              <span>系统管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/account"><i class="el-icon-picture"></i>修改密码</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container class="content">
        <Header />
        <div class="main">
          <router-view />
        </div>
        <Footer />
      </el-container>
    </el-container>
    <el-container class="container" v-else>
     <router-view/>
    </el-container>
  </div>
  
</template>

<script>
import Header from './components/Header.vue'
import Footer from './components/Footer.vue'
import { useRouter } from 'vue-router'
import { reactive, toRefs } from '@vue/reactivity'
import {localGet} from '@/utils'
import {pathMap} from '@/utils'
export default {
  name: 'App',
  components:{
    Header,
    Footer
  },
  setup(){
    //不需要菜单的路径数组
    const noMenu = ['/login']
    const router=useRouter()
    const state=reactive({
      showMenu:true,  //是否需要显示菜单
      defaultOpen:['1','2'],
      currentPath:'/'
    })
    //监听路由的变化
    router.beforeEach((to,from,next)=>{
      if(to.path=='/login'){
        //如果路径是/login，则正常执行
        next()
      }else {
        // 如果不是/login，判断是否有token
        if(!localGet('token')){
          next({path:'/login'})
        }else {
          next()
        }
      }
      // state.showMenu=!state.showMenu
      state.showMenu=!noMenu.includes(to.path)
      state.currentPath=to.path
      document.title=pathMap[to.name]
    })
    return {
      ...toRefs(state)
    }
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
  background-color: #ffffff;
}
.container {
  height: 100vh;
}
.aside {
  width: 200px!important;
  background-color: #222832;
}
.head {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
}
.head > div {
  display: flex;
  align-items: center;
}

.head img {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}
.head span {
  font-size: 20px;
  color: #ffffff;
}
.line {
  border-top: 1px solid hsla(0,0%,100%,.05);
  border-bottom: 1px solid rgba(0,0,0,.2);
}
.content {
    display: flex;
    flex-direction: column;
    max-height: 100vh;
    overflow: hidden;
  }
  .main {
    height: calc(100vh - 100px);
    overflow: auto;
    padding: 10px;
  }
</style>

<style>
body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.el-menu {
  border-right: none!important;
}
.el-submenu {
  border-top: 1px solid hsla(0, 0%, 100%, .05);
  border-bottom: 1px solid rgba(0, 0, 0, .2);
}
.el-submenu:first-child {
  border-top: none;
}
.el-submenu [class^="el-icon-"] {
  vertical-align: -1px!important;
}
a {
  color: #409eff;
  text-decoration: none;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
.el-popper__arrow {
  display: none;
}
</style>
